package com.demo.qywx.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.demo.qywx.model.UserCode;
import com.demo.qywx.model.UserInfo;
import com.demo.qywx.service.QyWxApiService;
import com.demo.qywx.util.ResultView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/front/weChat")
public class QywxChatController {

    @Autowired
    private QyWxApiService qyWxApiService;

    @RequestMapping(value = "/weChatLogin")
    public ResultView qyWechatLogin(String code){
        System.out.println(">>>>>>>>>>>>>>企业微信网页H5授权code>>>>>>>>>>>>>>>>>"+code);
        //获取token
        String token=qyWxApiService.getAppToken("","");
        System.out.println(">>>>>>>>>>>>>>获取access_tokentoken>>>>>>>>>>>>>>>>>"+token);
        if(StrUtil.isBlank(token)){
            return ResultView.error("获取access_token异常");
        }
        //code获取用户身份信息UserId详情
        UserCode userCode =qyWxApiService.getUserIdByCode(code,token);
        System.out.println(">>>>>>>>>>>>>>获取访问用户身份userId>>>>>>>>>>>>>>>>>"+ JSON.toJSONString(userCode));
        if(userCode==null){
            return ResultView.error("获取访问用户身份异常");
        }
        //根据tickit获取敏感信息
        UserInfo userInfo=qyWxApiService.getUserInfo(userCode.getUser_ticket());
        if(userInfo==null){
            return ResultView.error("获取用户敏感信息身份异常");
        }
        return ResultView.ok(userInfo);
    }


}
